<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends Services{
	private $status = TRUE;
	
	function Search(){
		parent::__construct();
				
	}
	
	
	protected function query(){
		$this->checkConsumerKey();
		$response_validate	= $this->validate('query');
		
		if($this->status){
			$this->load->model('search_model');
			$data	= $this->search_model->search(self::$params);
			if(is_array($data)){
				return $this->parseOutput($data);
			}else
				throw new NotExistException('pelicula');
		}
		else
			throw new UserValidateException(implode(',',$response_validate));
	}	
	
	private function validate_query(){
		
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$config	= array(
							array(
									'field' => 'filter_text',
									'label'	=> 'filter_text',
									'rules'	=> 'required'
							),
							array(
									'field' => 'limit',
									'label'	=> 'limit',
									'rules'	=> 'is_natural_no_zero'
							)
					);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	private function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	private function parseOutput($data=array(), $only_parse=false){
		if ($only_parse)
			return $this->{'_format_'.$this->getFormat()}($data);
		if($this->status==true){
			return $this->{'_trim_format_'.$this->getFormat()}(
					$this->{'_format_'.$this->getFormat()}($data)
					);
		}
    }
	
}